package com.zmj.demo.controller;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmj.demo.pojo.entity.req.RequestParam;
import com.zmj.framework.base.controller.BaseController;
import com.zmj.framework.common.CommonConstant;
import com.zmj.framework.request.PageRequest;
import com.zmj.framework.response.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zmj.demo.pojo.entity.MenuEntity;
import com.zmj.demo.service.MenuService;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author zmj.org
 * @since 2024-04-09
 */
@RestController
@Api(tags = { "MenuEntity" })
@RequestMapping("/v1/api/menuEntity")
public class MenuController extends BaseController<MenuService, MenuEntity> {

    @Autowired
    private MenuService menuService;

    @ApiOperation(value = "※ 新增或编辑保存")
    @Operation(summary = "※ 新增或编辑保存", description = "新增或编辑保存")
    @PostMapping(value = "/saveOrUpdate")
    public Result<?> saveOrUpdate(@Valid @RequestBody MenuEntity menuEntity) {
        baseService.saveOrUpdate(menuEntity);
        return Result.success();
    }

    @ApiOperation(value = "※ 根据id查询")
    @Operation(summary = "※ 根据id查询", description = "根据id查询")
    @GetMapping(value = "/getById/{id}")
    public Result getById(@PathVariable Integer id){
        Assert.notBlank(id.toString(),"id不能为空！");
        MenuEntity menuEntity = baseService.getById(id);
        return Result.success(menuEntity);
    }

    @ApiOperation(value = "根据id批量删除")
    @Operation(summary = "根据id批量删除", description = "根据id批量删除")
    @PostMapping(value = "/deleteByIds")
    public Result<?> deleteByIds(@RequestBody RequestParam requestParam) {
        String ids = requestParam.getIds();
        Assert.notBlank(ids,"ids不能为空！");
        return Result.success(menuService.removeBatchByIds(Arrays.stream(ids.split(",")).map(Integer::valueOf).collect(Collectors.toList())));
    }

    @ApiOperation(value = "※ 分页查询")
    @Operation(summary = "※ 分页查询", description = "分页查询")
    @PostMapping("/pageList")
    public Result<IPage<MenuEntity>> pageList(@RequestBody PageRequest pageRequest) {
        Map<String, Object> param = pageRequest.getParam();
        IPage<MenuEntity> page = new Page<>(pageRequest.getPageInfo().getPageIndex(), pageRequest.getPageInfo().getPageSize());
        page = menuService.page(page, Wrappers.<MenuEntity>lambdaQuery()
                .eq(MenuEntity::getIsDelete, CommonConstant.NO)
                .eq(ObjectUtil.isNotEmpty(param.get("name")), MenuEntity::getName, param.get("name"))
                .eq(ObjectUtil.isNotEmpty(param.get("type")), MenuEntity::getType, param.get("type"))
                .eq(ObjectUtil.isNotEmpty(param.get("visible")), MenuEntity::getVisible, param.get("visible"))
                .eq(ObjectUtil.isNotEmpty(param.get("status")), MenuEntity::getStatus, param.get("status"))
                .eq(ObjectUtil.isNotNull(param.get("parentId")), MenuEntity::getParentId, param.get("parentId")));
        return Result.success(page);
    }
}
